# loads the PBAR zspec data
#
#   3/??/2013, John Kwong

import numpy as np
import matplotlib.pyplot as plt
import numpy.matlib
import datetime
import time

filenameList = list()

plotColors = ['r', 'b', 'g', 'm', 'c', 'y', 'k']
lineStyles = ['-', '-.', '.']
markerTypes = ['.', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', 's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd']
datasetNumList = np.concatenate((np.arange(13,38), np.array([38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48])))

t = np.array([15, 20, 26, 31, 33, 39, 40, 44, 53, 56, 62, 68, 76, 80, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137])
badDetectors = np.concatenate((np.arange(1,9), t)) - 1
temp = np.zeros(137)
temp[badDetectors] = True
temp = temp.astype(bool)
goodDetectors = ~temp

goodDetectorsList = np.array(np.where(goodDetectors))
badDetectorsList = np.array(np.where(badDetectors))

fullfilenameList = list()
filenameList
for ii in datasetNumList:
    filenameList.append('dx' + str(ii) + '.csv')

datasetDescription = list()
datasetDescription.append('Col closed') # dx13
datasetDescription.append('Col closed') # 14
datasetDescription.append('Pb 2 inches') # 15
datasetDescription.append('Pb 4 inches') # 16
datasetDescription.append('Pb 3 inches') # 17
datasetDescription.append('Pb 5 inches') # 18
datasetDescription.append('Pb 6 inches') # 19
datasetDescription.append('Pb 7 inches') # 20
datasetDescription.append('Pb 8 inches') # 21
datasetDescription.append('Col closed') # 22
datasetDescription.append('Fe 4 inches') # 23
datasetDescription.append('Fe 6 inches') # 24
datasetDescription.append('Fe 8 inches') # 25
datasetDescription.append('Fe 10 inches') # 26
datasetDescription.append('Fe 12 inches') # 27
datasetDescription.append('Col closed') # 28
datasetDescription.append('Al 8 inches') # 29
datasetDescription.append('Al 12 inches') # 30
datasetDescription.append('Al 30 inches') # 31
datasetDescription.append('Al 28 inches') # 32
datasetDescription.append('Al 24 inches') # 33
datasetDescription.append('Al 20 inches') # 34
datasetDescription.append('Al 16 inches') # 35
datasetDescription.append('Col closed') # 36
datasetDescription.append('Col closed')  # dx37
datasetDescription.append('Col closed')  # dx38
datasetDescription.append('Col closed')  # dx39
datasetDescription.append('?')  #dx40
datasetDescription.append('?')  #dx41
datasetDescription.append('?')  #dx42
datasetDescription.append('?')  #dx43
datasetDescription.append('Col closed')  #dx44
datasetDescription.append('Col closed')  #dx45
datasetDescription.append('Col closed')  #dx46
datasetDescription.append('Col closed')  #dx47
datasetDescription.append('Col closed')  #dx48

datasetTime = list()
datasetTime.append('03/13/2013, 12:34')  #dx13
datasetTime.append('03/13/2013, 15:10')  #dx14
datasetTime.append('03/13/2013, 15:31')  #dx15
datasetTime.append('03/13/2013, 15:44')  #dx16
datasetTime.append('03/13/2013, 15:52')  #dx17
datasetTime.append('03/13/2013, 16:00')  #dx18
datasetTime.append('03/13/2013, 16:10')  #dx19
datasetTime.append('03/13/2013, 16:22')  #dx20
datasetTime.append('03/13/2013, 16:34')  #dx21
datasetTime.append('03/13/2013, 15:47')  #dx22
datasetTime.append('03/13/2013, 17:02')  #dx23
datasetTime.append('03/13/2013, 17:10')  #dx24
datasetTime.append('03/13/2013, 17:22')  #dx25
datasetTime.append('03/13/2013, 17:30')  #dx26
datasetTime.append('03/13/2013, 17:39')  #dx27
datasetTime.append('03/13/2013, 17:50')  #dx28
datasetTime.append('03/13/2013, 18:04')  #dx29
datasetTime.append('03/13/2013, 18:07')  #dx30
datasetTime.append('03/13/2013, 18:14')  #dx31
datasetTime.append('03/13/2013, 18:29')  #dx32
datasetTime.append('03/13/2013, 18:33')  #dx33
datasetTime.append('03/13/2013, 18:40')  #dx34
datasetTime.append('03/13/2013, 18:45')  #dx35
datasetTime.append('03/13/2013, 18:53')  #dx36
datasetTime.append('03/14/2013, 09:06')  #dx37
datasetTime.append('03/14/2013, 12:17')  #dx38
datasetTime.append('03/14/2013, 12:23')  #dx39
datasetTime.append('03/15/2013, 00:00')  #dx40
datasetTime.append('03/15/2013, 00:00')  #dx41
datasetTime.append('03/15/2013, 00:00')  #dx42
datasetTime.append('03/15/2013, 00:00')  #dx43
datasetTime.append('03/15/2013, 09:00')  #dx44
datasetTime.append('03/15/2013, 09:27')  #dx45
datasetTime.append('03/15/2013, 09:48')  #dx46
datasetTime.append('03/15/2013, 10:10')  #dx47
datasetTime.append('03/15/2013, 10:40')  #dx48

# convert to epoch time
datasetTimeNum = np.zeros(len(datasetTime))
for ii in range(0,len(datasetTime)):
    datasetTimeNum[ii] = time.mktime(datetime.datetime.strptime(datasetTime[ii], '%m/%d/%Y, %H:%M').timetuple())

datasetAcquisitionTime = np.array([600, 600, 120, 300, 120, 300, 500, 500, 500, 600, 120, 120, 300, 300, 300, 600, 120, 120, 500, 500, 300, 120, 120, 600, 600, 60, 60, 1, 1, 1, 1, 600, 600, 600, 600, 600])
datasetAcquisitionTime = datasetAcquisitionTime.astype(float)

basepath = r'N:\My Documents\Projects\PBAR\data'
basepath = r'C:\Users\jkwong\Documents\Work\PBAR\data'

for ii in range(len(filenameList)):
    fullfilenameList.append(basepath + '\\' + filenameList[ii])

dat = list()

for ii in range(len(fullfilenameList)):
    dat.append(np.genfromtxt(fullfilenameList[ii], \
                             delimiter=',', \
                             skip_header = 0, \
                             skip_footer = 0, \
                             dtype = 'uint32'))

# LISTS OF DATASETS
plotList_CollimatorClosed = np.array([1, 2, 10, 16, 24, 25, 32, 33, 34, 35, 36]) - 1

# in order of thickness
plotList_Pb = np.array([3, 5, 4, 6, 7, 8, 9]) - 1  # in order of thickness
plotList_Pb_Good = np.array([6, 7, 8, 9]) - 1 # not saturated
transmissionCorrectionPb = np.array([14.055,  52.7245, 181.654, 589.076, 1857.691, 3539.075, 4892.15])
transmissionCorrectionPb_Good = np.array([589.076, 1857.691, 3539.075, 4892.15])

plotList_Fe = np.array([11, 12, 13, 14, 15]) - 1  # 4, 6, 8, 10 ,12
plotList_Fe_Good = np.array([13, 14, 15]) - 1
transmissionCorrectionFe = np.array([14.930, 54.457, 173.391, 800, 1567.005])  # 800 is approximate value
transmissionCorrectionFe_Good = np.array([173.391, 800, 1567.005])


##plotList_Al = np.array([17, 18, 19, 20, 21, 22, 23 ]) - 1
plotList_Al = np.array([17, 18, 23, 22, 21, 20, 19]) - 1
plotList_Al_Good = np.array([21, 20, 19]) - 1
transmissionCorrectionAl = np.array([5.035, 12.128, 26.172, 56.370, 111.874, 228.550, 309.308])
transmissionCorrectionAl_Good = np.array([111.874, 228.550, 309.308])

# Calculate gain correction for zpsec data



